<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="detailList.categories">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/">{{ detailList.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ detailList.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ detailList.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 新增结构 -->
        <!-- 图片预览区 -->
        <div class="media">
          <GoodImg :goodImg="detailList.mainPictures" />
          <GoodSales />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 商品基本信息 -->
          <GoodName />
          <!-- 商品规格 sku组件 -->
          <GoodsSku :goods="detailList" @change="getSku" />
          <!-- 数量组件 -->
          <XtxNumbox v-model="buyNum" :max="detailList.inventory" />
          <!-- 按钮组件 -->
          <XtxButton
            @click="addCart"
            style="margin-top: 20px"
            size="large"
            type="primary"
          >
            加入购物车
          </XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <div class="goods-tabs">
            <!-- 详情图片列表 -->
            <img
              v-for="(item, i) in detailList.details.pictures"
              :key="i"
              :src="item"
            />
          </div>
        </div>
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, provide } from 'vue'
import { findGoods } from '@/api/good.js'
import { useRoute } from 'vue-router'
import GoodImg from './components/goods-image.vue'
import GoodSales from './components/goods-sales'
import GoodName from './components/goods-name'
import msg from '@/components/Message/index.js'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodImg,
    GoodSales,
    GoodName
  },
  setup () {
    const store = useStore()
    const detailList = ref([])
    provide('detailList', detailList)
    const route = useRoute()
    // 传递给子组件的数量
    const buyNum = ref(1)
    const getDeatilList = async () => {
      const res = await findGoods(route.params.id)
      detailList.value = res
      console.log(res)
    }
    getDeatilList()
    // 商品sku
    const currSel = ref(null)
    const getSku = (currSku) => {
      console.log(currSku)
      if (currSku.skuId) {
        detailList.value.price = currSku.price
        detailList.value.oldPrice = currSku.oldPrice
        detailList.value.inventory = currSku.inventory
        currSel.value = currSku
      } else {
        currSel.value = null
      }
    }
    const addCart = async () => {
      if (!currSel.value) {
        return msg({ text: '请填写完整的商品规格' })
      }
      if (currSel.value.inventory === 0) {
        return msg({ text: '此规格商品已经卖完了' })
      }
      const addGood = {
        id: detailList.value.id,
        name: detailList.value.name,
        picture: detailList.value.mainPictures[0],
        skuId: currSel.value.skuId,
        price: currSel.value.oldPrice,
        nowPrice: currSel.value.price,
        attrsText: currSel.value.specsText,
        stock: currSel.value.inventory,
        selected: true,
        isEffective: true,
        count: buyNum.value
      }
      console.log(addGood)
      const rmsg = await store.dispatch('cart/setGoodListAction', addGood)
      msg({ type: 'success', text: rmsg })
    }
    return { detailList, addCart, getSku, buyNum }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  // 新增样式
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
  img {
    width: 100%;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
